package com.baidumap.administrator.happystudy.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.baidumap.administrator.happystudy.R;

import java.util.ArrayList;
import java.util.List;

/**
 * author: C on 2017/3/21.
 * gridview适配器
 */
public class MyGridViewAdapter extends BaseAdapter{
    private Context mContext;
    private List<String> names = null;
    private int[] pics = new int[]{
            R.mipmap.xiyi1,
            R.mipmap.xiyi2,
            R.mipmap.xiyi3,
            R.mipmap.xiyi4,
            R.mipmap.meishi1,
            R.mipmap.meishi2,
            R.mipmap.meishi3,
            R.mipmap.xiyi1,
            R.mipmap.meishi3

    };

    public MyGridViewAdapter(Context context) {
        mContext = context;
        names = new ArrayList<>();
        names.add("存储卡");
        names.add("读卡器");
        names.add("滤镜");
        names.add("闪光灯");
        names.add("相机包");
        names.add("三脚架");
        names.add("相机清理");
        names.add("相机贴膜");
        names.add("机身配件");
    }

    @Override
    public int getCount() {
        return names.size();
    }

    @Override
    public Object getItem(int position) {
        return names.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
        if (convertView == null) {
            viewHolder = new ViewHolder();
            convertView = LayoutInflater.from(mContext).inflate(R.layout.adapter_gridview_my, null);
            viewHolder.tvName = (TextView)convertView.findViewById(R.id.tv_name);
            viewHolder.ivPic = (ImageView)convertView.findViewById(R.id.iv_pic);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder)convertView.getTag();
        }
        viewHolder.ivPic.setImageResource(pics[position]);
        viewHolder.tvName.setText(names.get(position));

        return convertView;
    }

    class ViewHolder {
        private TextView tvName;
        private ImageView ivPic;
    }

}
